package com.yuncheng.spcyApi.spcy;

import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.bo.JcydjbSetGzdwParamBo;
import com.yuncheng.spcyApi.bo.ParamsBo;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.spcyService.SpcyJcydjbService;
import com.yuncheng.spcyApi.constant.jcy.JcydjbConstant;
import com.yuncheng.spcyApi.entity.SpcyRsJcydjb;
import com.yuncheng.spcyApi.entity.SpcyRsJcydjfbwz;
import com.yuncheng.spcyApi.entity.SpcyRsJcydjgzjl;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @Description: 审评查验-检查员登记表-相关信息
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Api(tags = "审评查验-检查员登记表相关信息")
@RestController
@RequestMapping("/api/spzx-spcy/spcyJcydjb")
public class SpcyJcydjbController {

    @Resource
    private SpcyJcydjbService spcyJcydjbService;

    @Log(title = "分页查询-检查员登记表")
    @ApiOperation(value = "分页查询-检查员登记表")
    @GetMapping(value = "queryPageDjb")
    public HttpResult queryPageDjb(SpcyRsJcydjb jcydjb,
                                   @RequestParam(value = "year",defaultValue = "")String year,
                                   @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                   @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        return spcyJcydjbService.queryPageDjb(jcydjb, year, pageNo, pageSize);
    }

    @Log(title = "新增文章-根据登记表id")
    @ApiOperation(value = "新增文章-根据登记表id")
    @GetMapping(value = "addDjwz")
    public HttpResult addDjwz(@RequestParam(value = "id",defaultValue = "") String id){

        SpcyRsJcydjfbwz jcydjfbwz = spcyJcydjbService.addDjwz(id);

        return HttpResult.ok(jcydjfbwz);
    }

    @Log(title = "新增工作经历-根据登记表id")
    @ApiOperation(value = "新增工作经历-根据登记表id")
    @GetMapping(value = "addDjgzjl")
    public HttpResult addDjgzjl(@RequestParam(value = "id",defaultValue = "") String id){

        SpcyRsJcydjgzjl jcydjgzjl = spcyJcydjbService.addDjgzjl(id);
        return HttpResult.ok(jcydjgzjl);
    }

    @Log(title = "更新检查员登记表-根据登记表id")
    @ApiOperation(value = "更新检查员登记表-根据登记表id")
    @PutMapping(value = "updateJcydjb")
    public HttpResult updateJcydjb(@RequestBody SpcyRsJcydjb buffer) throws InvocationTargetException, IllegalAccessException {

        spcyJcydjbService.updateJcydjb(buffer);
        return HttpResult.ok("保存成功");
    }

    @Log(title = "更新发表文章-根据文章id")
    @ApiOperation(value = "更新发表文章-根据文章id")
    @PutMapping(value = "updateWz")
    public HttpResult updateWz(@RequestBody SpcyRsJcydjfbwz bean) throws InvocationTargetException, IllegalAccessException {

        spcyJcydjbService.updateWz(bean);
        return HttpResult.ok("保存成功");
    }

    @Log(title = "更新工作经历-根据工作经历id")
    @ApiOperation(value = "更新工作经历-根据工作经历id")
    @PutMapping(value = "updateGzjl")
    public HttpResult updateGzjl(@RequestBody SpcyRsJcydjgzjl bean) throws InvocationTargetException, IllegalAccessException {

        spcyJcydjbService.updateGzjl(bean);
        return HttpResult.ok("保存成功");
    }

    @Log(title = "更新检查员类别-根据类别id")
    @ApiOperation(value = "更新检查员类别-根据类别id")
    @PutMapping(value = "updateLb")
    public HttpResult updateLb(@RequestBody HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException {

        spcyJcydjbService.updateLb(buffer);
        return HttpResult.ok("保存成功");
    }

    @Log(title = "删除发表文章-根据文章id")
    @ApiOperation(value = "删除发表文章-根据文章id")
    @DeleteMapping(value = "deleteWz")
    public HttpResult deleteWz(@RequestParam(value = "id",defaultValue = "") String id){

        spcyJcydjbService.deleteWz(id);

        return HttpResult.ok("删除成功");
    }

    @Log(title = "删除工作经历-根据工作经历id")
    @ApiOperation(value = "删除工作经历-根据工作经历id")
    @DeleteMapping(value = "deleteGzjl")
    public HttpResult deleteGzjl(@RequestParam(value = "id",defaultValue = "") String id){

        spcyJcydjbService.deleteGzjl(id);

        return HttpResult.ok("删除成功");
    }

    @Log(title = "提交-检查员登记表-根据登记表id")
    @ApiOperation(value = "提交-检查员登记表-根据登记表id")
    @GetMapping(value = "submitJcydjb")
    public HttpResult submitJcydjb(@RequestParam(value = "id",defaultValue = "") String id){

        spcyJcydjbService.submitJcydjb(id);

        return HttpResult.ok("提交成功");
    }

    @Log(title = "废弃-检查员登记表-根据登记表id")
    @ApiOperation(value = "废弃-检查员登记表-根据登记表id")
    @DeleteMapping(value = "fqJcydjb")
    public HttpResult fqJcydjb(@RequestParam(value = "id",defaultValue = "") String id){

        spcyJcydjbService.fqJcydjb(id);

        return HttpResult.ok("删除成功");
    }

    @Log(title = "退回填写")
    @ApiOperation(value = "退回填写")
    @PostMapping(value = "updateSjcjzt")
    public HttpResult updateSjcjzt(@RequestBody ParamsBo paramsBo){

        spcyJcydjbService.updateSjcjztYtjToTxz(paramsBo);
        return HttpResult.ok("修改成功");
    }

    @Log(title = "提交预览")
    @ApiOperation(value = "提交预览")
    @GetMapping(value = "submitPreview")
    public HttpResult submitPreview(@RequestParam(value = "id",defaultValue = "")String id){

        spcyJcydjbService.submitPreview(id);
        return HttpResult.ok("提交成功");
    }

    @Log(title = "预览退回")
    @ApiOperation(value = "预览退回")
    @GetMapping(value = "previewReturn")
    public HttpResult previewReturn(@RequestParam(value = "id",defaultValue = "")String id){

        spcyJcydjbService.previewReturn(id);
        return HttpResult.ok("退回成功");
    }

    @Log(title = "身份证验证重载-检查员登记表-根据登记表id")
    @ApiOperation(value = "身份证验证重载-检查员登记表-根据登记表id")
    @GetMapping(value = "verifyJcydjb")
    public HttpResult verifyJcydjb(@RequestParam(value = "fsfzh",defaultValue = "") String fsfzh){

        /*if (StringUtils.isBlank(code)){
            return HttpResult.error("请输入验证码！");
        }*/
        Map map = spcyJcydjbService.initReloadJcydjb(fsfzh, null, "3781");

        return HttpResult.ok(map);
    }

    @Log(title = "根据身份证号获取手机号")
    @ApiOperation(value = "根据身份证号获取手机号")
    @GetMapping(value = "getPhoneBySfzh")
    public HttpResult getPhoneBySfzh(@RequestParam(value = "fsfzh",defaultValue = "") String fsfzh){

        String phone = spcyJcydjbService.getPhoneBySfzh(fsfzh);

        HttpResult result = new HttpResult(true);
        result.setResult(phone);
        result.setMessage("获取成功");
        return result;
    }

    @Log(title = "发送验证码")
    @ApiOperation(value = "发送验证码")
    @GetMapping(value = "sendYzm")
    public HttpResult sendYzm(@RequestParam(value = "fsfzh",defaultValue = "") String fsfzh,
                              @RequestParam(value = "phone",defaultValue = "") String phone) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {

        /*String flag = spcyJcydjbService.getSysConfigByCode(JcydjbConstant.SYS_CONFIG_QRCODE_SECODE_STATE);
        flag = Optional.ofNullable(flag).orElseGet(()->"1");
        if (StringUtils.isBlank(flag) || flag.equals("1")){
            throw new BusinessException("二维码已失效！");
        }*/
        spcyJcydjbService.sendYzm(fsfzh,phone);
        return HttpResult.ok("发送成功");
    }

    @Log(title = "查询-检查员登记表-根据登记表id")
    @ApiOperation(value = "查询-检查员登记表-根据登记表id")
    @GetMapping(value = "queryDjbById")
    public HttpResult queryDjbById(@RequestParam(value = "id",defaultValue = "") String id){

        Map map = spcyJcydjbService.initReloadJcydjb(null, id, null);

        return HttpResult.ok(map);
    }

    @Log(title = "下载检查员登记表-根据登记表id")
    @ApiOperation(value = "下载检查员登记表-根据登记表id")
    @GetMapping(value = "replateFile")
    public void replateFile(@RequestParam(value = "id",defaultValue = "") String id, HttpServletResponse response){

        spcyJcydjbService.downloadDjbFile(id, false, response);
    }

    @Log(title = "预览检查员登记表-根据登记表id")
    @ApiOperation(value = "预览检查员登记表-根据登记表id")
    @GetMapping(value = "previewFile")
    public void previewFile(@RequestParam(value = "id",defaultValue = "") String id, HttpServletResponse response){

        spcyJcydjbService.downloadDjbFile(id,  false, response);
    }

    @Log(title = "重新下载检查员登记表-根据登记表id")
    @ApiOperation(value = "重新下载检查员登记表-根据登记表id")
    @GetMapping(value = "reloadReplateFile")
    public void reloadReplateFile(@RequestParam(value = "id",defaultValue = "") String id, HttpServletResponse response){

        spcyJcydjbService.downloadDjbFile(id, true, response);
    }

    @Log(title = "导出登记表excel表格详情-根据登记ids或条件")
    @ApiOperation(value = "出登记表excel表格详情-根据登记ids或条件")
    @GetMapping(value = "exportExcelJcydjxq")
    public void exportExcelJcydjxq(SpcyRsJcydjb jcydjb,
                                   ParamsBo paramsBo,
                                   @RequestParam(value = "startTime",defaultValue = "")String startTime,
                                   @RequestParam(value = "endTime",defaultValue = "")String endTime,
                                   HttpServletResponse response){
        spcyJcydjbService.exportExcelJcydjxq(jcydjb, paramsBo, startTime, endTime, response);
    }

    @Log(title = "导出所有已提交检查员类别的zip文件")
    @ApiOperation(value = "导出所有已提交检查员类别的zip文件")
    @GetMapping(value = "exportAllJcylbExcelZip")
    public void exportAllJcylbExcelZip(@RequestParam(value = "fsjcjzt",defaultValue = "")String fsjcjzt,
                                       @RequestParam(value = "startTime",defaultValue = "")String startTime,
                                       @RequestParam(value = "endTime",defaultValue = "")String endTime,
                                       HttpServletResponse response){
        if (StringUtils.isBlank(fsjcjzt)){
            fsjcjzt = JcydjbConstant.SJCJZT_YTJ;
        }
        spcyJcydjbService.exportAllJcylbExcelZip(fsjcjzt,startTime,endTime,response);
    }

    @Log(title = "验证二维码是否有效")
    @ApiOperation(value = "验证二维码是否有效")
    @GetMapping(value = "verifyQrCodeState")
    public HttpResult verifyQrCodeState(){
        boolean state = true;
        /*String flag = spcyJcydjbService.getSysConfigByCode(JcydjbConstant.SYS_CONFIG_QRCODE_SECODE_STATE);
        flag = Optional.ofNullable(flag).orElseGet(()->"1");
        if (StringUtils.isBlank(flag) || flag.equals("1")){
            state = false;
        }*/

        HttpResult result = new HttpResult(true);
        result.setResult(state);
        return result;
    }

    @Log(title = "数据对比")
    @ApiOperation(value = "数据对比")
    @PostMapping(value = "dataComparison")
    public void dataComparison(@RequestParam("file")MultipartFile file, HttpServletResponse response){
        spcyJcydjbService.dataComparison(file,response);
    }

    @Log(title = "数据对比-导出对应信息")
    @ApiOperation(value = "数据对比-导出对应信息")
    @PostMapping(value = "dataComparisonExportInfo")
    public void dataComparisonExportInfo(@RequestParam("file")MultipartFile file, HttpServletResponse response){
        spcyJcydjbService.dataComparisonExportInfo(file,response);
    }

    @Log(title = "数据对比-修改检查员类别")
    @ApiOperation(value = "数据对比-修改检查员类别")
    @PostMapping(value = "dataComparisonLb")
    public void dataComparisonLb(@RequestParam("file")MultipartFile file,@RequestParam(value = "fileType",defaultValue = "")String fileType){
        spcyJcydjbService.dataComparisonLb(file,fileType);
    }

    @Log(title = "数据对比-工作单位")
    @ApiOperation(value = "数据对比-工作单位")
    @PostMapping(value = "dataComparisonGzdw")
    public void dataComparisonGzdw(@RequestParam("file")MultipartFile file){
        spcyJcydjbService.dataComparisonGzdw(file);
    }

    @Log(title = "查询所有有效的检查员工作单位列表")
    @ApiOperation(value = "查询所有有效的检查员工作单位列表")
    @GetMapping(value = "queryAllJcyGzdwList")
    public HttpResult queryAllJcyGzdwList(){
        return HttpResult.ok(spcyJcydjbService.queryAllJcyGzdwList());
    }

    @Log(title = "查询所有有效的检查员工作单位列表")
    @ApiOperation(value = "查询所有有效的检查员工作单位列表")
    @PostMapping(value = "batchSetJcyShzt")
    public HttpResult batchSetJcyShzt(@RequestBody JcydjbSetGzdwParamBo paramBo){

        spcyJcydjbService.batchSetJcyShzt(paramBo);
        return HttpResult.ok("设置成功");
    }
}
